package cn.gqr.service.impl;

import cn.gqr.entity.ClientDeta;
import cn.gqr.mapper.ClientMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.stereotype.Service;

import java.util.Arrays;

@Service
public class ClientDetailsServiceImpl implements ClientDetailsService {

    @Autowired
    private ClientMapper cm;

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {

        ClientDeta client_deta = cm.selectById(clientId);

        if(client_deta ==null){
            throw new ClientRegistrationException("此客户端信息不存在");
        }

        BaseClientDetails client = new BaseClientDetails();//系统默认实现子类
        client.setClientId(client_deta.getClientId());   //客户端id
        client.setClientSecret(client_deta.getClientSecret());//客户端密码
        client.setAuthorizedGrantTypes(Arrays.asList(client_deta.getAuthorizationType()));//授权类型
        client.setScope(Arrays.asList(client_deta.getScope()));//授权范围
        client.setAccessTokenValiditySeconds(60);//token过期时间
        client.setAutoApproveScopes(client.getScope());//自动处理

        return client;
    }
}
